package com.uchengwang.ui.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;

import java.util.Arrays;
import java.util.List;

/**
 * Created by U城网 on 2017/3/15.
 * LinearLayout根据list添加布局
 */

public class LayoutAddViewUtils {

    private Context context;
    private OnViewItemListener listener;

    public LayoutAddViewUtils(Context context){
        this.context = context;
    }

    public LayoutAddViewUtils addView(LinearLayout layout, String[] strings){
        List<String> stringList = Arrays.asList(strings);
        addView(layout, stringList);
        return this;

    }

    public LayoutAddViewUtils addView(LinearLayout layout, List<String> strings){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        for (int i = 0; i<strings.size(); i++){
            View view = LayoutInflater.from(context).inflate(R.layout.fast_release_item,null);
            TextView textView = (TextView) view.findViewById(R.id.fast_item_text);
            textView.setText(strings.get(i));
            final int finalI = i;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null){
                        listener.onViewItem(view, finalI);
                    }

                }
            });

            layout.addView(view, params);
        }

        return this;

    }

    //设置点击事件
    public void setOnViewItemListener(OnViewItemListener onViewItemListener){
        listener = onViewItemListener;

    }



    /**
     * position 标记点击的位置
     * */
    public interface OnViewItemListener{
        void onViewItem(View view, int position);
    }
}
